﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Silverlight.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Windows.Threading;

using Caliburn.Micro.BikeWizard.Utils;

namespace Caliburn.Micro.BikeWizard.Tests
{
    /// <summary>
    /// Simple test to verify the correct working of the MultipleEventWaiter 
    /// 
    /// Todo:
    /// 
    /// 1) Can't get rid of the magic strings with event names. Any ideas?
    /// </summary>
    [TestClass]
    public class EventWaiterTests : SilverlightTest
    {
        [TestMethod]
        [Asynchronous]
        public void MultipleEventWaiterSuccess()
        {          
            // Simply wait until 2 Tick events have been received (default timeout is 10 seconds, so that should be fine)
            // The preActionToPerform delegate starts the timer
            // the postActionToPerform delegate stops the timer and signals that the test was succesful
            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 1);
            MultipleEventWaiter eventWaiter =
                new MultipleEventWaiter(
                    () => timer.Start(),
                    (error) =>
                    {
                        timer.Stop();
                        
                        if (error == null)
                            EnqueueTestComplete();
                    },
                    new SingleEventWaiter(timer, "Tick", 2));

            eventWaiter.Execute(null);
        }

        [TestMethod]
        [Asynchronous]
        [ExpectedException(typeof(Exception))]
        public void MultipleEventWaiterTimeout()
        {
            // Simply wait until 2 Tick events have been received
            // Only wait 2 seconds for the 15 Tick events to fire. That won't work :) 
            // The preActionToPerform delegate starts the timer
            // the postActionToPerform delegate stops the timer and signals that the test was succesful
            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 1);
            MultipleEventWaiter eventWaiter =
                new MultipleEventWaiter(
                    () => timer.Start(),
                    (error) =>
                    {
                        timer.Stop();

                        if (error == null)
                            EnqueueTestComplete();
                    },
                    new SingleEventWaiter(timer, "Tick", 15, new TimeSpan(0,0,2)));

            eventWaiter.Execute(null);
        }
    }
}
